package cc.laop.java.concurrent;

import java.util.concurrent.Phaser;

/**
 * @Auther: Pengpeng
 * @Date: create in 2018/1/11 18:52
 * @Description:
 */
public class PhaserTest {

    public static class Student extends Thread {
        private Phaser ps;

        public Student(Phaser ps) {
            this.ps = ps;
        }

        public void run() {
            try {
                answer("first question");
                answer("second question");
                answer("third question");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void answer(String question) throws InterruptedException {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread() + ": finish " + question);
            ps.arriveAndAwaitAdvance();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Phaser ps = new Phaser(10);
        for (int i = 0; i < 10; i++) {
            new Student(ps).start();
        }
        Thread.sleep(10000);
    }
}
